<template>
  <div class="category_wrap">
    <!-- <el-card class="category_wrap_card"> 类别管理 </el-card> -->
    <div class="category_top_box">
      <el-card class="category_top_card">
        <el-form :inline="true" class="demo-form-inline">
          <!-- 药品 -->
          <el-form-item label="类别名称">
            <el-input v-model="queryCategoryListParam.name" placeholder="请输入类别名称" clearable style="width: 200px" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" style="position: relative; left: -8px" @click="querySelectClick">
              <el-icon style="margin-right: 6px"><Search /></el-icon>
              查询
            </el-button>
            <el-button type="primary" @click="addEditClick">
              <el-icon style="margin-right: 6px"><Plus /></el-icon>
              添加
            </el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <div class="category_bottom_box">
      <el-card class="category_bottom_card">
        <el-table :data="categoryList" border style="width: 100%" v-loading="isTableLoading" stripe>
          <!-- 序号 -->
          <el-table-column prop="serialNumber" label="序号" width="80" align="center" />

          <!-- 类别名称 -->
          <el-table-column prop="name" label="名称" align="center" show-overflow-tooltip />

          <!-- 创建者 -->
          <el-table-column prop="creator" label="创建者" align="center" show-overflow-tooltip />

          <!-- 更新者 -->
          <el-table-column prop="updator" label="更新者" align="center" show-overflow-tooltip />

          <!-- 创建时间 -->
          <!-- <el-table-column prop="gmtCreate" label="创建者" align="center" show-overflow-tooltip /> -->

          <!-- 更新时间 -->
          <!-- <el-table-column prop="gmtModify" label="更新者" align="center" show-overflow-tooltip /> -->
        </el-table>

        <div class="pagination_box">
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :current-page="queryCategoryListParam.page"
            :page-size="queryCategoryListParam.size"
            @current-change="pageChange"
            @size-change="sizeChange"
          />
        </div>
      </el-card>
    </div>

    <!-- 添加类别弹窗 -->
    <el-dialog v-model="isShowAddCategoryDialog" title="添加类别" top="8vh" width="460" draggable>
      <div class="add_form_box">
        <el-form
          label-position="right"
          label-width="100px"
          ref="addCategoryFormRef"
          :model="addCategoryFormData"
          :rules="addCategoryFormRules"
        >
          <!-- 类别名称 -->
          <el-form-item label="类别名称" prop="name">
            <el-input placeholder="请输入类别名称" v-model="addCategoryFormData.name" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="addCategoryDialogCancelClick">取消</el-button>
          <el-button type="primary" @click="addCategoryDialogConfirmClick(addCategoryFormRef)"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
export default defineComponent({
  name: "Category"
});
</script>
<script lang="ts" setup>
import { CategoryListType } from "@/api/modules/category/type";
import { selectTbGoodsCategoryByPageApi, addTbGoodsCategoryApi } from "@/api/modules/category/index";
import { timestampToDate } from "@/utils/day";

// 添加表单校验规则
const addCategoryFormRules = ref({
  name: [{ required: true, message: "请输入药品名称", trigger: "blur" }]
});

// 显示添加弹窗标识
const isShowAddCategoryDialog = ref(false);

// 添加类别表单Ref
const addCategoryFormRef = ref<any>(null);

// 添加类别表单数据
const addCategoryFormData = ref({
  name: ""
});

// 添加类别弹窗取消点击事件
const addCategoryDialogCancelClick = () => {
  isShowAddCategoryDialog.value = false;
  addCategoryFormRef.value.resetFields();
};

// 添加类别弹窗确定点击事件
const addCategoryDialogConfirmClick = async formRef => {
  if (!formRef) return;
  await formRef.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      addCategoryFunc();
      isShowAddCategoryDialog.value = false;
      formRef.resetFields();
    } else {
      console.log("error submit!", fields);
    }
  });
};

// 添加类别请求函数
const addCategoryFunc = async () => {
  await addTbGoodsCategoryApi(addCategoryFormData.value)
    .then(res => {
      ElMessage.success("添加成功");
    })
    .catch(err => {
      ElMessage.error("添加失败");
    });
};

// 添加点击事件
const addEditClick = () => {
  isShowAddCategoryDialog.value = true;
};

// 查询点击事件
const querySelectClick = () => {
  queryCategoryListParam.value.page = 1;
  selectTbGoodsCategoryByPageFunc();
};

// 表格加载标识
const isTableLoading = ref(false);

// 类别列表
let categoryList = ref<CategoryListType>([]);

// 页码change事件
const pageChange = e => {
  console.log("page", e);
  queryCategoryListParam.value.page = e;
  selectTbGoodsCategoryByPageFunc();
};

// 最大条数change事件
const sizeChange = e => {
  console.log("size", e);
  queryCategoryListParam.value.size = e;
  selectTbGoodsCategoryByPageFunc();
};

// 查询类别列表参数数据
const queryCategoryListParam = ref({
  page: 1,
  size: 15,
  name: ""
});

// 类别列表总条数
const total = ref(0);

// 分页查询类别列表的函数
const selectTbGoodsCategoryByPageFunc = async () => {
  isTableLoading.value = true;
  await selectTbGoodsCategoryByPageApi(queryCategoryListParam.value)
    .then(res => {
      // 计算序号
      res.data.list.forEach((cate, cateIndex) => {
        cate.serialNumber = (queryCategoryListParam.value.page - 1) * queryCategoryListParam.value.size + (cateIndex + 1);
        cate.gmtCreate = timestampToDate(cate.gmtCreate);
        cate.gmtModify = timestampToDate(cate.gmtModify);
      });
      categoryList.value = res.data.list;
      total.value = res.data.total;
    })
    .finally(() => {
      isTableLoading.value = false;
    });
};

// init
onMounted(() => {
  selectTbGoodsCategoryByPageFunc();
});
</script>

<style lang="scss" scoped>
.category_wrap {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;

  // border: 1px solid red;
  .category_top_box {
    width: 100%;
    height: 70px;
    .category_top_card {
      // padding-left: 10px;
      width: 100%;
      height: 100%;
    }
  }
  .category_bottom_box {
    flex: 1;
    width: 100%;
    margin-top: 12px;

    // border: 1px solid red;
    .category_bottom_card {
      width: 100%;
      height: 100%;
      .pagination_box {
        display: flex;
        justify-content: flex-end;
        margin-top: 16px;
      }
    }
  }
  .add_form_box {
    padding-top: 40px;
    padding-right: 20px;
    padding-bottom: 20px;
  }
}
:deep(.category_bottom_card .el-card__body) {
  padding: 14px;
}
</style>
